package operlog

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"yj-app/app/model"
	operlogModel "yj-app/app/model/monitor/oper_log"
	operlogService "yj-app/app/service/monitor/operlog"
	"yj-app/app/service/utils/response"
)

//用户列表页
func List(r *ghttp.Request) {
	response.WriteTpl(r, "monitor/operlog/list.html")
}

//用户列表分页数据
func ListAjax(r *ghttp.Request) {
	var req *operlogModel.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		r.Response.WriteJsonExit(model.CommonRes{
			Code: 500,
			Msg:  err.Error(),
		})
	}

	rows := make([]operlogModel.Entity, 0)

	result, page, err := operlogService.SelectPageList(req)

	if err == nil && result != nil {
		rows = *result
	}

	r.Response.WriteJsonExit(model.TableDataInfo{
		Code:  0,
		Msg:   "操作成功",
		Total: page.Total,
		Rows:  rows,
	})
}

//清空记录
func Clean(r *ghttp.Request) {

	rs := operlogService.DeleteRecordAll()

	if rs > 0 {
		response.SucessDataDel(r, "清空操作日志", "all", rs)
	} else {
		response.ErrorDelData(r, "清空操作日志", "all", 0)
	}
}

//删除数据
func Remove(r *ghttp.Request) {
	var req *model.RemoveReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorMsg(r, "删除操作日志", req, model.Buniss_Del, err.Error())
	}

	rs := operlogService.DeleteRecordByIds(req.Ids)

	if rs > 0 {
		response.SucessDataDel(r, "删除操作日志", req, rs)
	} else {
		response.ErrorDelData(r, "删除操作日志", req, 0)
	}
}

//记录详情
func Detail(r *ghttp.Request) {

	id := r.GetQueryInt64("id")

	if id <= 0 {
		response.WriteTpl(r, "error/error.html", g.Map{
			"desc": "参数错误",
		})
		return
	}

	operLog, err := operlogService.SelectRecordById(id)

	if err != nil {
		response.WriteTpl(r, "error/error.html", g.Map{
			"desc": "数据不存在",
		})
		return
	}

	response.WriteTpl(r, "monitor/operlog/detail.html", g.Map{
		"operLog": operLog,
	})
}

//导出
func Export(r *ghttp.Request) {
	var req *operlogModel.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorMsg(r, "导出登陆日志", req, model.Buniss_Other, err.Error())
	}
	url, err := operlogService.Export(req)

	if err != nil {
		response.ErrorMsg(r, "导出登陆日志", req, model.Buniss_Other, err.Error())
	} else {
		response.SucessMsg(r, "", req, model.Buniss_Other, url)
	}
}
